package com.lastproject.ejb.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.QuestionOptionDTO;
import com.lastproject.ejb.entities.QuestionOption;
import com.lastproject.ejb.util.QuestionOptionConversion;

/**
 * Session Bean implementation class QuestionOptionDAO
 * 
 * Author:Alina
 */
@Stateless
public class QuestionOptionDAO implements QuestionOptionDAORemote {

	/**
	 * Default constructor.
	 */
	@PersistenceContext
	private EntityManager em;
	@EJB
	QuestionOptionConversion conv;

	public QuestionOptionDAO() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void create(QuestionOptionDTO entity) {
		em.persist(conv.fromDTO(entity));

	}

	@Override
	public void edit(QuestionOptionDTO entity) {
		QuestionOption questionOption = em.find(QuestionOption.class, conv
				.fromDTO(entity).getId());
		em.merge(questionOption);

	}

	@Override
	public void remove(QuestionOptionDTO entity) {
		QuestionOption questionOption = em.find(QuestionOption.class, conv
				.fromDTO(entity).getId());
		em.remove(questionOption);

	}

	@Override
	public QuestionOptionDTO findById(int id) {
		QuestionOption questionOption = em.find(QuestionOption.class, id);
		return conv.fromEntity(questionOption);
	}

	@Override
	public List<QuestionOptionDTO> findAll() {
		List<QuestionOption> questions = em.createQuery(
				"SELECT qo FROM QuestionOption qo", QuestionOption.class)
				.getResultList();

		List<QuestionOptionDTO> questionsList = new ArrayList<QuestionOptionDTO>();

		for (QuestionOption question : questions) {
			QuestionOptionDTO qo = new QuestionOptionDTO();
			qo = conv.fromEntity(question);
			questionsList.add(qo);

		}

		return questionsList;
	}

	@Override
	public List<QuestionOptionDTO> getQuestionOptionListByQuestionId(int id) {
		List<QuestionOption> questions = em
				.createQuery(
						"SELECT qo FROM QuestionOption qo JOIN qo.question AS q WHERE q.id =:id",
						QuestionOption.class).setParameter("id", id)
				.getResultList();
		List<QuestionOptionDTO> questionOptionsListByQuestionId = new ArrayList<QuestionOptionDTO>();

		for (QuestionOption question : questions) {
			QuestionOptionDTO qo = new QuestionOptionDTO();
			qo = conv.fromEntity(question);
			questionOptionsListByQuestionId.add(qo);

		}

		return questionOptionsListByQuestionId;
	}

	@Override
	public List<QuestionOptionDTO> getQuestionOptionNumericListByQuestionId(
			int id) {
		List<QuestionOption> questions = em
				.createQuery(
						"SELECT qo FROM QuestionOption qo JOIN qo.question AS q WHERE ((qo.type='singleChoice' OR qo.type='multipleChoice' OR qo.type='grade') AND q.id =:id)",
						QuestionOption.class).setParameter("id", id)
				.getResultList();
		List<QuestionOptionDTO> questionOptionsListByQuestionId = new ArrayList<QuestionOptionDTO>();

		for (QuestionOption question : questions) {
			QuestionOptionDTO qo = new QuestionOptionDTO();
			qo = conv.fromEntity(question);
			questionOptionsListByQuestionId.add(qo);

		}

		return questionOptionsListByQuestionId;
	}

}
